package com.shang.controller;

import com.shang.common.CommonResult;
import com.shang.entity.Result;
import com.shang.entity.UserMark;
import com.shang.service.ResultService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@RestController
@Slf4j
@EnableSwagger2
@RequestMapping(value = "/result")
public class ResultController {

    @Autowired
    private ResultService resultService;

    @RequestMapping(value = "/getUserMark",method = RequestMethod.GET)
    @ApiOperation(value = "getUserMark",notes = "获取关注列表")
    public CommonResult getUserMark(@RequestParam String userName){
        CommonResult commonResult = new CommonResult();
        try {
            List<UserMark> userMarks = resultService.getUserMark(userName);
            commonResult=CommonResult.success(userMarks);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/updateSolved",method = RequestMethod.GET)
    @ApiOperation(value = "updateSolved",notes = "获取关注列表")
    public CommonResult updateSolved(@RequestParam long comment_id){
        CommonResult commonResult = new CommonResult();
        try{
            int updateSolved = resultService.updateSolved(comment_id);
            if (updateSolved==1){
                commonResult=CommonResult.success();
            }
            else {
                return commonResult.fail("更新失败！");
            }
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/deleteUserMark",method = RequestMethod.GET)
    @ApiOperation(value = "deleteUserMark",notes = "获取关注列表")
    public CommonResult deleteUserMark(@RequestParam long comment_id){
        CommonResult commonResult = new CommonResult();
        try{
            String msg = resultService.deleteUserMark(comment_id);
            commonResult=CommonResult.success(msg);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
}

    @RequestMapping(value = "/addResultScore",method = RequestMethod.GET)
    @ApiOperation(value = "addResultScore",notes = "获取关注列表")
    public CommonResult addResultScore(@RequestParam String date,int beforeValue,int afterValue){
        CommonResult commonResult = new CommonResult();
        try{
            int addResult = resultService.addResultScore(date, beforeValue, afterValue);
            if (addResult==1){
                commonResult=CommonResult.success();
            }
            else {
                return commonResult.fail("出错了");
            }
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/getResultByDate",method = RequestMethod.GET)
    @ApiOperation(value = "getResultByDate",notes = "获取关注列表")
    public CommonResult getResultByDate(@RequestParam String date){
        CommonResult commonResult = new CommonResult();
        try{
            Result resultByDate = resultService.getResultByDate(date);
            commonResult=CommonResult.success(resultByDate);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

}